/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.example.ui.model
{

import org.example.ui.component.LoginComponent;
import org.example.ui.component.supportClasses.LoginComponentState;
import org.example.ui.event.LoginEvent;
import org.teotigraphix.dx.core.dx_internal;
import org.teotigraphix.dx.patterns.model.ComponentModelCore;

/**
 * @copyright Teoti Graphix, LLC
 * @author Michael Schmalle
 * @date 07-29-09
 */
public class LoginComponentModelCore 
	   extends ComponentModelCore
	   implements ILoginComponentModel
{
	//--------------------------------------------------------------------------
	//
	//  ILoginComponent API :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  loggingInText
	//----------------------------------
	
	[Bindable]
	
	/**
	 * @copy org.example.ui.component.ILoginComponent#loggingInText
	 */
	public var loggingInText:String = "Logging in...";
	
	//----------------------------------
	//  loggedInText
	//----------------------------------
	
	[Bindable]
	
	/**
	 * @copy org.example.ui.component.ILoginComponent#loggedInText
	 */	
	public var loggedInText:String = "Login Successful";
	
	//----------------------------------
	//  loginMessageText
	//----------------------------------
	
	[Bindable]
	
	/**
	 * @copy org.example.ui.component.ILoginComponent#loginMessageText
	 */
	public var loginMessageText:String = "";
	
	//----------------------------------
	//  loginLabel
	//----------------------------------
	
	[Bindable]
	
	/**
	 * @copy org.example.ui.component.ILoginComponent#loginLabel
	 */
	public var loginLabel:String = "Login";
	
	//----------------------------------
	//  passwordLabel
	//----------------------------------
	
	[Bindable]
	
	/**
	 * @copy org.example.ui.component.ILoginComponent#passwordLabel
	 */
	public var passwordLabel:String = "Password";
	
	//--------------------------------------------------------------------------
	//
	//  ILoginComponentModel API :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  loggingIn
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _loggingIn:Boolean;

	/**
	 * @copy org.example.ui.model.ILoginComponentModel#loggingIn
	 */
	public function get loggingIn():Boolean
	{
		return _loggingIn;
	}
	
	/**
	 * @private
	 */
	public function set loggingIn(value:Boolean):void
	{
		_loggingIn = value;
		
		invalidateSkin();
			
		// TODO: add validation
		component.dispatchEvent( new LoginEvent(
				LoginEvent.LOGIN, 
				login.loginInput.text, 
				login.passwordInput.text) );
	}	
	
	//----------------------------------
	//  loggedIn
	//----------------------------------
	
	private var _loggedIn:Boolean;

	/**
	 * @copy org.example.ui.model.ILoginComponentModel#loggedIn
	 */
	public function get loggedIn():Boolean
	{
		return _loggedIn;
	}
	
	/**
	 * @private
	 */
	public function set loggedIn(value:Boolean):void
	{
		_loggedIn = value;
		
		invalidateSkin();
		
		component.dispatchEvent( new LoginEvent(
				LoginEvent.LOGIN_COMPLETE, 
				login.loginInput.text, 
				login.passwordInput.text) );		
	}
	
	//--------------------------------------------------------------------------
	//
	//  Protected :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  login
	//----------------------------------
	
	/**
	 * @private
	 */
	protected function get login():LoginComponent
	{
		return component as LoginComponent;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function LoginComponentModelCore()
	{
		super();
		
		dx_internal::initializeComponent = true;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Public :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @inheritDoc
	 */
	override public function listValueInterests():Array
	{
		return [
			"loggingInText", 
			"loggedInText", 
			"loginLabel",
			"passwordLabel",
			"loginMessageText"];
	}
	
	/**
	 * @inheritDoc
	 */
	override public function getSkinState():String 
	{
		if(loggingIn)
			return LoginComponentState.LOGGING_IN;
		
		if(loggedIn)
			return LoginComponentState.LOGGED_IN;
		
		return LoginComponentState.LOGIN;
	}
	
	//--------------------------------------------------------------------------
	//
	//  ILoginComponent API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy org.example.ui.component.ILoginComponent#loginSucceeded()
	 */
	public function loginSucceeded():void 
	{
		_loggingIn = false;
		// fires LOGIN_COMPLETE event
		loggedIn = true;
	}
	
	/**
	 * @copy org.example.ui.component.ILoginComponent#loginFailed()
	 */
	public function loginFailed():void 
	{
		_loggingIn = _loggedIn = false;
		invalidateSkin();
	}
	
	//--------------------------------------------------------------------------
	//
	//  ILoginComponentModel API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy org.example.ui.model.ILoginComponentModel#loggedIn
	 */
	public function invalidateLoginMessage():void 
	{
		var pendingSkinState:String = getSkinState();
			
		if (login.currentState != pendingSkinState)
		 {
		 	// Logging in...
			if (pendingSkinState == LoginComponentState.LOGGING_IN)
				loginMessageText = login.loggingInText;
			// Login Successful
			else if (pendingSkinState == LoginComponentState.LOGGED_IN)
				loginMessageText = login.loggedInText;
		}
		else 
		{
			// Logging in...
			if (login.currentState == LoginComponentState.LOGGING_IN && 
				loginMessageText != login.loggingInText)
				loginMessageText = login.loggingInText;
			// Login Successful
			else if (login.currentState == LoginComponentState.LOGGED_IN && 
				loginMessageText != login.loggedInText)
				loginMessageText = login.loggedInText;
		}
	}
}
}